package com.lagou.java.handler;

import com.lagou.java.RpcRequest;
import com.lagou.java.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @Classname UserServiceHandler
 * @Description TODO
 * @Date 2020/7/22 12:59
 * @Created by pc
 */
public class UserServiceHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // 如果符合规定, 则调用本地方法, 返回数据
        if (msg instanceof RpcRequest) {
            RpcRequest request = (RpcRequest) msg;
            Object invoke = ServerBootstrap.invoke(
                    request.getClassName(),
                    request.getMethodName(),
                    request.getParameters());
            ctx.writeAndFlush(invoke.toString());
            System.out.println("result msg is : " + invoke);
        }

//         msg:  UserService#sayHello#are you ok?
//        if(msg.toString().startsWith("UserService")){
//            UserServiceImpl userService = new UserServiceImpl();
//            String result = userService.sayHello(msg.toString().substring(msg.toString().lastIndexOf("#") + 1));
//            ctx.writeAndFlush(result);
//        }

    }

}
